```{r}
# Data Preparation:
# Each sheet (for 2015, 2016, and 2017) is read, variables are renamed to English, and all interactions are calculated explicitly for clarity.

# Model Specification:
# The regression includes main effects, all two-way and the three-way interaction for the triple difference estimator, plus division and year fixed effects via dummy variables.

# Regressions:
# For each placebo year, a separate triple-DID model is estimated (see formula construction), and models are stored in a list indexed by year.

# Output Table:
# Uses the stargazer package to produce a combined, clearly labelled, and omission-customized output.

# Covariate labels are set for clarity, using ‘×’ for interactions.
# Fixed effects are omitted from the main table for readability but acknowledged in a footnote.
# File Output:
# Results are exported in HTML format (optionally, also as a .tex file via stargazer).

# placebo_triple_diff_combined_EN.R

# --- Packages ---
library(readxl)
library(dplyr)
library(lmtest)
library(sandwich)
library(stargazer)

# --- File path and placebo worksheet names ---
excel_file <- "Data Placebo Test3.xlsx"
sheets     <- c("2015", "2016", "2017")

# --- List to store models ---
models <- list()

# --- Loop: estimate each placebo regression and store results ---
for (sheet in sheets) {
  df <- read_excel(excel_file, sheet = sheet) %>%
    rename(
      Employment = Yit_Emprego,
      TAX        = NSit_NaoSimples,
      FalsePost  = POSLEIt_Post
    ) %>%
    mutate(
      PAY                 = as.integer(Situacao == "Reonerado"),
      TAXxPAY             = TAX * PAY,
      TAXxFalsePost       = TAX * FalsePost,
      PAYxFalsePost       = PAY * FalsePost,
      TAXxPAYxFalsePost   = TAX * PAY * FalsePost
    )
  
  # Identify fixed-effects dummy variables
  div_vars  <- grep("^DummyDivCNAE_", names(df), value = TRUE)
  year_vars <- grep("^DummyAno_",     names(df), value = TRUE)
  
  # Build the regression formula: main variables and fixed effects
  main_vars <- c("TAX", "PAY", "FalsePost",
                 "TAXxPAY", "TAXxFalsePost", "PAYxFalsePost", "TAXxPAYxFalsePost")
  all_vars  <- c(main_vars, div_vars, year_vars)
  fmla      <- as.formula(paste0("Employment ~ -1 + ", paste(all_vars, collapse = " + "))) # -1 removes the intercept
  
  # Fit the model and store it
  models[[sheet]] <- lm(fmla, data = df)
}

# --- Create combined summary table (Stargazer) ---

stargazer(
  models[["2015"]], models[["2016"]], models[["2017"]],
  title            = "Placebo Tests: Triple Differences – Employment",
  type             = "html",
  out              = "Placebo_All_stargazer.html",
  digits           = 2,
  column.labels    = c("Placebo 2015", "Placebo 2016", "Placebo 2017"),
  covariate.labels = c(
    "TAX",
    "PAY",
    "False Post-treatment",
    "TAX × PAY",
    "TAX × False Post-treatment",
    "PAY × False Post-treatment",
    "TAX × PAY × False Post-treatment"
  ),
  omit             = c("DummyDivCNAE", "DummyAno"),
  omit.stat        = c("f", "ser"),
  add.lines        = list(c("Fixed effects", "CNAE divisions and years")),
  header           = FALSE
)

# Optionally, output with simpler column names:
stargazer(
  models[["2015"]], models[["2016"]], models[["2017"]],
  title            = "Placebo Tests: Triple Differences – Employment",
  type             = "html",
  out              = "Placebo_All_stargazer.html",
  digits           = 2,
  column.labels    = c("2015", "2016", "2017"),
  covariate.labels = c(
    "TAX",
    "PAY",
    "False Post-treatment",
    "TAX × PAY",
    "TAX × False Post-treatment",
    "PAY × False Post-treatment",
    "TAX × PAY × False Post-treatment"
  ),
  omit             = c("DummyDivCNAE", "DummyAno"),
  omit.stat        = c("f", "ser"),
  add.lines        = list(c("Fixed effects", "CNAE divisions and years")),
  header           = FALSE
)

message("Combined summary table saved as Placebo_All_stargazer.html and .tex")
```

